// pages/clickMe/clickMe.js
const Controllers = require('../controllers/Controller').controller;
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar:[],
    globalStype:'',
  	integralpagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
    shopId:'',
    dialogShow:false,
    buttons:[{text: '取消'}, {text: '确定'}],
    integralGoodsId:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
    this.setData({
      myInformationdata:app.globalData.myInformationdata
    })
    if(option.invitationAccountId){
      this.setData({
        invitationAccountId:option.invitationAccountId
      })
    }
    if(option.shopId){
      console.log(option)
      let shopId=option.shopId
      app.globalData.shopId=option.shopId
      wx.setStorageSync('shopId', shopId)
      this.setData({
        shopId:shopId
      })
    }
    if(wx.getStorageSync('shopId')){
      this.setData({
        shopId:wx.getStorageSync('shopId')
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('usertoken')) {
      //加载首页数据
      this.myInformation()
      if(this.data.invitationAccountId){
        this.invitationPerson()
      }
    }else{
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if(res.data.sessionId){
          this.myInformation()
          if(this.data.invitationAccountId){
            this.invitationPerson()
          }
        }
      }
    }
    this.setData({
      integralpagedata:[],
      pageNumber:1,
      pageSize:10,
      isEnd:false,
      isloading:true,
    })
    if(app.globalData.shopId){
      this.setData({
        'malllists[0].shopTypeId':app.globalData.shopId,
        shopId:app.globalData.shopId,
      })
      this.integralpage()
    }
  },
  integralpage(){
    Controllers.integralpage({
      pageSize:this.data.pageSize,
      pageNumber:this.data.pageNumber,
      shopId:this.data.shopId
    }).then(res => {
      let list=res.data.list
      if(list.length>0){
        // for (let i = 0; i < list.length; i++) {
        //   if (list[i].couponNum <=0 && list[i].type==2) {
        //     // list.splice(i, 1);
        //     delete list[i]
        //   }
        // }
        list = list.filter(item => {
          if(item.type!=2 || item.couponNum>0 ||  item.haveNum==0)return item
        });
       
        this.setData({
          integralpagedata:this.data.integralpagedata.concat(list),

        })
        
      }
      if(res.data.lastPage){
          this.setData({
            isEnd:true
          })
        }
      this.setData({
        isloading:false
      })
    });
  },
  changeintegral(e){
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
  	Controllers.exchange({
      integralGoodsId:this.data.integralGoodsId,
      shopId:this.data.shopId
    }).then(res => {
	    wx.showToast({
	      title: '兑换成功！',
	      icon: 'none',
	      duration: 1000
      })
      
      setTimeout(function(){
        wx.navigateTo({
          url: '/pages/my/product-order/product-order',
        })
      },1000)

      this.setData({
        integralpagedata:[],
        pageNumber:1,
        pageSize:10,
        isEnd:false,
        isloading:true,
      })
      this.integralpage()
    }).catch(res=>{
	    // wx.showToast({
	    //   title: res.desc || '下单失败',
	    //   icon: 'none'
	    // })
	  })
  },
  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
          pageNumber: this.data.pageNumber + 1,
      });
      this.integralpage()
    }
  },


  myInformation(){
    //获取数据
    Controllers.myInformation({shopId:this.data.shopId}).then(res => {
      this.setData({
        myInformationdata:res.data.accountRelation
      })
      app.globalData.myshopinfo=res.data.accountRelation
    })
  },

  //打开二次确认弹框
  openDialog(e){
    this.setData({
      dialogShow:true,
      integralGoodsId:e.currentTarget.dataset.id,
    })
  },


  //二次弹框确认
  tapDialogButton(e){
    let index=e.detail.index
    if(index==0){
      this.setData({
        dialogShow:false,
        integralGoodsId:""
      })
    }else{
      this.setData({
        dialogShow:false,
      })
      this.changeintegral()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let obj = {
      title: this.data.shopDetailsdata.shopName,
      path:'/pages/clickMe/clickMe?shopId=' + this.data.shopId+"&invitationAccountId="+this.data.myInformationdata.accountId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },



  toActivityDetail(){
    wx.navigateTo({
      url: '/pages/clickMe/activityDetail/activityDetail',
    })
  },

  //绑定上级
  invitationPerson(){
    let value={}
    value.invitationAccountId=this.data.invitationAccountId
    value.shopId=wx.getStorageSync('shopId')
    Controllers.invitationPerson(value).then(res=>{
        
    })
  }
})